<template>
    <div>
        <el-dialog title="新建商机" width="900px" :visible.sync="dialogVisible">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="110px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item prop="name" label="商机名称">
                            <el-input v-model="form.name" type="text" placeholder="请输入" style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="user_id" label="客户">
                            <el-select v-model="form.user_id" :disabled="userDisabled" filterable remote placeholder="请选择" style="width: 100%;" :remote-method="getUserList">
                                <el-option v-for="item, index in userList" :key="index" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="预计放款金额">
                            <el-input v-model="form.loan" type="text" placeholder="请输入" style="width: 100%;">
                                <template slot="append">万元</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="commission" label="预计回佣金额">
                            <el-input v-model="form.commission" type="text" placeholder="请输入" style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="times" label="预计签单日期">
                            <el-date-picker
                                v-model="form.times"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="请选择签单日期"
                                style="width: 100%;"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="status" label="商机阶段">
                            <el-select v-model="form.status" placeholder="请选择" style="width: 100%;">
                                <el-option label="匹配方案" :value="0" />
                                <el-option label="强调优势" :value="1" />
                                <el-option label="签订合同" :value="2" />
                                <el-option label="进行审批" :value="3" />
                                <el-option label="银行放款" :value="4" />
                                <el-option label="回款" :value="5" />
                                <el-option label="赢单" :value="6" />
                                <el-option label="输单" :value="7" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="跟进人">
                            <el-select v-model="form.member_id" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="(item, index) in memberList" :key="index" :label="item.realname" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { createBusiness } from '@/api/business'
import { memberList } from '@/api/system'
import { userList } from '@/api/kehu'
export default {
    name: 'UserAddDialog',
    data() {
        return {
            dialogVisible: false,
            userDisabled: false,
            form: {
                name: '',
                user_id: '',
                loan: '',
                commission: '',
                times: '',
                status: '',
                member_id: ''
            },
            memberList: [],
            userList: [],
            rules: {
                name: [{ required: true, message: '请输入商机名称', trigger: 'change' }],
                user_id: [{ required: true, message: '请选择客户', trigger: 'change' }],
                pool: [{ required: true, message: '请选择商机阶段组', trigger: 'change' }],
                times: [{ required: true, message: '请选择签单日期', trigger: 'change' }],
                commission: [{ required: true, message: '请输入佣金', trigger: 'change' }],
                status: [{ required: true, message: '请选择商机阶段', trigger: 'change' }]
            }
        }
    },
    methods: {
        showDialog(item) {
            this.dialogVisible = true;
            this.resetParams();
            this.resetForm();
            this.getOptions();
            this.getUserList();
            if (item.user_id) {
                this.$nextTick(() => {
                    this.form.user_id = item.user_id;
                    this.userDisabled = true
                })
                
            }
        },
        resetForm() {
            this.$nextTick(() => {
                this.$refs.ruleForm.resetFields();
            })
        },
        resetParams() {
            this.form = {
                name: '',
                user_id: '',
                loan: '',
                commission: '',
                times: '',
                status: '',
                member_id: ''
            }
            this.userDisabled = false;
        },
        handleSubmit(e) {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    createBusiness(this.form).then(res => {
                        if (res.status == '1') {
                            this.$message.success('商机创建成功！')
                            this.$emit('success', res)
                        } else {
                            this.$message.error(res.message)
                        }
                    }).finally(() => {
                        this.dialogVisible = false
                    })
                }
            })
        },
        getUserList(query) {
            if (query !== '') {
                const params = { pages: 1, limit: 50, keys: query || '' };
                userList(params).then(res => {
                    this.userList = res.data.list
                })
            } else {
                this.userList = [];
            }
        },
        // 获取 options
        getOptions() {
            memberList().then(res => {
                this.memberList = res.data.list || []
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.tag-item {
    margin-right: 5px;
}
</style>